package com.es.poem.controller;


import com.es.poem.config.ResultInfo;
import com.es.poem.util.RedisUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("login")
public class LoginController {


    @Resource
    private RedisUtil redisUtil;


    //登录
    @PostMapping("/login")
    public ResultInfo login(@RequestParam("username") String username,@RequestParam("password") String password){
        ResultInfo resultInfo = new ResultInfo();
        //账号密码验证

        //设置token
        String token=UUID.randomUUID().toString().replace("-","")+new Date().getTime();
        redisUtil.set("ESToken@"+username, token);

        return resultInfo.success(token);
    }

    @GetMapping("/verification")
    public ResultInfo verification(@RequestParam("username") String username,@RequestParam("token") String token){
        ResultInfo resultInfo = new ResultInfo();

        Object o = redisUtil.get("ESToken@" + username);
        if(Objects.isNull(o)){
            return resultInfo.success(false);
        }
        //验证token
        String redisToken=String.valueOf(o);
        if(redisToken.equals(token)){
            return resultInfo.success(true);
        }

        return resultInfo.success(false);
    }

    public static void main(String[] args){
    }
}
